package com.tiandao.mofang.core.jianying;

import com.tiandao.mofang.core.RobotSingleton;
import com.tiandao.mofang.core.role.BaseRole;
import com.tiandao.mofang.core.util.ColorUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class DuoJiNengThread implements Runnable{
    private static Robot robot = RobotSingleton.getInstance();

    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(800);
                switch (isDuoJiNeng(1)){
                    case 1 :
                        BaseRole.rollLeft();
                        break;
                    case 2 :
                        BaseRole.rollRight();
                        break;
                    case 3 :
                        BaseRole.rollFront();
                        break;
                    case 4 :
                        BaseRole.rollAfter();
                        break;
                    case 5 :
                        BaseRole.rollAfter();
                        BaseRole.rollAfter();
                        break;
                    case 0 :
                        break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
//        int i =1;
//        while (true){
//            try {ssss
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            BufferedImage img = robot.createScreenCapture(new Rectangle(700, 470, 520, 360));
//            try {
//                ImageIO.write(img, "png", new File("D:/jina/剑影"+i+".png"));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            i++;
//        }
        while(true){
            try {
                Thread.sleep(800);
                switch (isDuoJiNeng(1)){
                    case 1 :
                        BaseRole.rollLeft();
                        break;
                    case 2 :
                        BaseRole.rollRight();
                        break;
                    case 3 :
                        BaseRole.rollFront();
                        break;
                    case 4 :
                        BaseRole.rollAfter();
                        break;
                    case 5 :
                        BaseRole.rollAfter();
                        BaseRole.rollAfter();
                        break;
                    case 0 :
                        break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    //躲技能判断
    //返回一个数字 1左 2右 3上 4下 0不 5翻滚两次
    public static int isDuoJiNeng(int j){
        //截图
        BufferedImage img = robot.createScreenCapture(new Rectangle(700, 490, 520, 360));
//        BufferedImage img = null;
//        try {
//            img = ImageIO.read(new File("D://jina/剑影"+j+".png"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        //分析红色系
        int[] rgbS = img.getRGB(0,0,520,360,null,0,520);
        int r = 0;
        int g = 0;
        int b = 0;
        int h = 0;
        int top = 0;
        int bottom = 0;
        int left = 0;
        int right= 0;
        //红色系计数
        int weiNum = 0;
        //循环判断所有像素点
//        System.out.println(rgbS.length);
        for(int i = 0;i<rgbS.length;i++){
            Color myColor = new Color(rgbS[i]);
            r = myColor.getRed();
            g = myColor.getGreen();
            b = myColor.getBlue();
            h = ColorUtil.rgb2h(r,g,b);
            if(h>325){
                weiNum++;
            }
            if(h>=0&&h<=15){
                weiNum++;
            }
            if(i==3380||i==3900||i==4420||i==4940){
                if(h>325){
                    top = 1;
                }
                if(h>=0&&h<=15){
                    top = 1;
                }
            }
            if(i==182260||i==182780||i==183300||i==183820){
                if(h>325){
                    bottom = 1;
                }
                if(h>=0&&h<=15){
                    bottom = 1;
                }
            }
            if(i==93650||i==93651||i==93652||i==93653){
                if(h>325){
                    left = 1;
                }
                if(h>=0&&h<=15){
                    left = 1;
                }
            }
            if(i==94066||i==94067||i==94068||i==94069){
                if(h>325){
                    right = 1;
                }
                if(h>=0&&h<=15){
                    right = 1;
                }
            }

        }
        System.out.println("红色系数量: 剑影"+j+"    "+weiNum);
        System.out.println("上: "+top+ "下: "+bottom +"左: "+left+"右: "+right);
        //返回boolean值
        if(weiNum>55000){
            if(bottom!=1){
                return 4;
            }
            if(left!=1){
                return 1;
            }
            if(right!=1){
                return 2;
            }
            if(top!=1){
                return 3;
            }
            return 5;

        }
        return 0;
    }
}
